home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Flock 0.9.1.3 stable / flock-0.9.1.3.en-US.win32.exe / flock / components / flockLoggingService.js < prev    next >
Text File  |  2007-10-12  |  7KB  |  206 lines

  1. //
  2. // BEGIN FLOCK GPL
  3. // 
  4. // Copyright Flock Inc. 2005-2007
  5. // http://flock.com
  6. // 
  7. // This file may be used under the terms of of the
  8. // GNU General Public License Version 2 or later (the "GPL"),
  9. // http://www.gnu.org/licenses/gpl.html
  10. // 
  11. // Software distributed under the License is distributed on an "AS IS" basis,
  12. // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. // for the specific language governing rights and limitations under the
  14. // License.
  15. // 
  16. // END FLOCK GPL
  17. //
  18.  
  19. const CLASS_ID                = Components.ID("{E8C889A6-F832-4B09-A216-66A7E6178896}");
  20. const CLASS_NAME              = "Flock Logging Service";
  21. const CONTRACT_ID             = "@flock.com/logging-service;1";
  22.  
  23. const flockILoggingService    = Components.interfaces.flockILoggingService;
  24.  
  25. function flockLoggingService()
  26. {
  27.   this.setLevel(flockILoggingService.LEVEL_ALL);
  28.  
  29.   this._observers = new Array();
  30.   
  31.   this._enabled = true;
  32.  
  33.   var catmgr = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
  34.   var enum = catmgr.enumerateCategory("flockILoggingObserver");
  35.   while(enum.hasMoreElements()) {
  36.     try {
  37.       var obj = enum.getNext();
  38.       var supportsString = obj.QueryInterface(Components.interfaces.nsISupportsCString);
  39.       var shortName = supportsString.toString();
  40.       var cid = catmgr.getCategoryEntry("flockILoggingObserver", shortName); 
  41.       var svc = Components.classes[cid].getService(Components.interfaces.flockILoggingObserver);
  42.       this.addObserver(svc);
  43.     } catch(e) {}
  44.   }
  45.   
  46.   var obs = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  47.   obs.addObserver(this, "profile-after-change", false);
  48. }
  49.  
  50. flockLoggingService.prototype = {
  51.  
  52.   _observers: null,
  53.   _level: null,
  54.   _enabled: null,
  55.  
  56.   log: function(aLevel, aContext, aMessage)
  57.   {
  58.     if (this._enabled) {
  59.         if (aLevel >= this._level) {
  60.           var date = new Date();
  61.           for(var i = 0; i < this._observers.length; i++) {
  62.             this._observers[i].emit(date.getTime(), aLevel, aContext, aMessage);
  63.           }
  64.         }
  65.       }
  66.   },
  67.   
  68.   setLevel: function(aLevel) {
  69.     this._level = aLevel;
  70.   },
  71.   
  72.   addObserver: function(aObserver)
  73.   {
  74.     if (aObserver == null) {
  75.       throw Components.Exception("Observer is null!!");
  76.     }
  77.     this._observers.push(aObserver);
  78.   },
  79.   
  80.   removeObserver: function(aObserver)
  81.   {
  82.     for(var i = 0; i < this._observers.length; i++) {
  83.       if(aObserver == this._observers[i]) {
  84.         this._observers.splice(i,1);
  85.         break;
  86.       }
  87.     }
  88.   },
  89.   
  90.   // nsIObserver
  91.   observe: function(subject, topic, state)
  92.   {
  93.     switch (topic) {
  94.       case "profile-after-change":
  95.         var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2);
  96.         // watch prefs for changes
  97.         prefService.addObserver("flock.service.logger.enabled", this, false);
  98.         prefService.addObserver("flock.service.logger.level", this, false);
  99.         // init with current prefs
  100.         this.observe(null, "nsPref:changed", null);
  101.         break;
  102.       case "nsPref:changed":
  103.         var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  104.         if (prefService.getPrefType("flock.service.logger.enabled")) {
  105.           this._enabled = prefService.getBoolPref("flock.service.logger.enabled");
  106.         } else {
  107.           this._enabled = true;
  108.         }
  109.         if (prefService.getPrefType("flock.service.logger.level")) {
  110.           this.setLevel(prefService.getIntPref("flock.service.logger.level"));
  111.         } else {
  112.           this.setLevel(flockILoggingService.LEVEL_WARN);
  113.         }
  114.         break;
  115.     }
  116.   },
  117.   
  118.   // nsIClassInfo
  119.   getInterfaces: function(aCount)
  120.   {
  121.     var interfaces = [Components.interfaces.flockILoggingService, Components.interfaces.nsIClassInfo, Components.interfaces.nsIObserver];
  122.     aCount.value = interfaces.length;
  123.     return interfaces;
  124.   },
  125.  
  126.   // nsIClassInfo
  127.   getHelperForLanguage: function(aLanguage)
  128.   {
  129.     return null;
  130.   },
  131.  
  132.   // nsIClassInfo
  133.   contractID: CONTRACT_ID,
  134.  
  135.   // nsIClassInfo
  136.   classDescription: CLASS_NAME,
  137.  
  138.   // nsIClassInfo
  139.   classID: CLASS_ID,
  140.  
  141.   // nsIClassInfo
  142.   implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  143.  
  144.   // nsIClassInfo
  145.   flags: Components.interfaces.nsIClassInfo.SINGLETON,
  146.   
  147.   // nsISupports
  148.   QueryInterface: function(aIID)
  149.   {
  150.     if (!aIID.equals(Components.interfaces.nsISupports) && !aIID.equals(Components.interfaces.flockILoggingService) && !aIID.equals(Components.interfaces.nsIClassInfo) && !aIID.equals(Components.interfaces.nsIObserver))
  151.       throw Components.results.NS_ERROR_NO_INTERFACE;
  152.     return this;
  153.   }
  154.  
  155. };
  156.  
  157. /******************************************************************************
  158.  * XPCOM Functions for construction and registration
  159.  ******************************************************************************/
  160. var Module = {
  161.   _firstTime: true,
  162.   registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  163.   {
  164.     if (this._firstTime) {
  165.       this._firstTime = false;
  166.       throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  167.     }
  168.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  169.     aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
  170.     
  171.     // Make the Logging Service a startup observer
  172.     var categoryManager = Components.classes["@mozilla.org/categorymanager;1"]
  173.       .getService(Components.interfaces.nsICategoryManager);
  174.     categoryManager.addCategoryEntry("app-startup", CLASS_NAME, "service," + CONTRACT_ID, true, true);
  175.   },
  176.  
  177.   unregisterSelf: function(aCompMgr, aLocation, aType)
  178.   {
  179.     aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  180.     aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);        
  181.   },
  182.   
  183.   getClassObject: function(aCompMgr, aCID, aIID)
  184.   {
  185.     if (!aIID.equals(Components.interfaces.nsIFactory))
  186.       throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  187.     if (aCID.equals(CLASS_ID))
  188.       return Factory;
  189.     throw Components.results.NS_ERROR_NO_INTERFACE;
  190.   },
  191.  
  192.   canUnload: function(aCompMgr) { return true; }
  193. };
  194.  
  195. var Factory = {
  196.   createInstance: function(aOuter, aIID)
  197.   {
  198.     if (aOuter != null)
  199.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  200.     return (new flockLoggingService()).QueryInterface(aIID);
  201.   }
  202. };
  203.  
  204. function NSGetModule(aCompMgr, aFileSpec) { return Module; }
  205.  
  206.